/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.sys.service.adv;

import java.util.List;

import com.thinkgem.jeesite.common.service.TreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.sys.entity.adv.HkCategory;
import com.thinkgem.jeesite.modules.hk.sys.dao.adv.HkCategoryDao;
import com.thinkgem.jeesite.modules.hk.sys.entity.adv.HkCategoryQuery;
import com.thinkgem.jeesite.modules.hk.sys.dao.adv.HkCategoryQueryDao;

/**
 * 乐看系统分类Service
 * @author 罗天文
 * @version 2018-04-19
 */
@Service
@Transactional(readOnly = true)
public class HkCategoryService extends TreeService<HkCategoryDao, HkCategory> {

	@Autowired
	private HkCategoryQueryDao hkCategoryQueryDao;
	
	public HkCategory get(String id) {
		HkCategory hkCategory = super.get(id);
		if(null!=hkCategory) {
			hkCategory.setHkCategoryQueryList(hkCategoryQueryDao.findList(new HkCategoryQuery(hkCategory)));
		}
		return hkCategory;
	}
	
	public List<HkCategory> findList(HkCategory hkCategory) {
		if (StringUtils.isNotBlank(hkCategory.getParentIds())){
			hkCategory.setParentIds(","+hkCategory.getParentIds()+",");
		}return super.findList(hkCategory);
	}
	
	public Page<HkCategory> findPage(Page<HkCategory> page, HkCategory hkCategory) {
		return super.findPage(page, hkCategory);
	}
	
	@Transactional(readOnly = false)
	public void save(HkCategory hkCategory) {
		super.save(hkCategory);
		for (HkCategoryQuery hkCategoryQuery : hkCategory.getHkCategoryQueryList()){
			if (hkCategoryQuery.getId() == null){
				continue;
			}
			if (HkCategoryQuery.DEL_FLAG_NORMAL.equals(hkCategoryQuery.getDelFlag())){
				if (StringUtils.isBlank(hkCategoryQuery.getId())){
					hkCategoryQuery.setCategory(hkCategory);
					hkCategoryQuery.preInsert();
					hkCategoryQueryDao.insert(hkCategoryQuery);
				}else{
					hkCategoryQuery.preUpdate();
					hkCategoryQueryDao.update(hkCategoryQuery);
				}
			}else{
				hkCategoryQueryDao.delete(hkCategoryQuery);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkCategory hkCategory) {
		super.delete(hkCategory);
		hkCategoryQueryDao.delete(new HkCategoryQuery(hkCategory));
	}
	
}